#include "gamewindow.h"
#include "ui_gamewindow.h"
#include "qpainter.h"
#include "qevent.h"
#include"qpoint.h"
#include "qmessagebox.h"
#include<qtimer.h>
#include<QPushButton>
#include<QMediaPlayer>

gamewindow::gamewindow(QWidget *parent) :
    QWidget(parent),
    limit_time(500),
    timer(new QTimer(this)),

    ui(new Ui::gamewindow)
{
    ui->setupUi(this);
    //ui->mainToolBar->hide();
    //ui->menuBar->hide();
        this->setGeometry(QRect(650,300,(CHESS_COLUMES+10)*RECT_WIDTH,(CHESS_ROWS+1)*RECT_HEIGHT));
        mIsBlackTurn=true;
        mIsWhichBg=1;
        connect(timer,SIGNAL(timeout()),this,SLOT(countdown()));
        temp_time=limit_time+1;
        timer->start(1000);//倒计时每一秒跳动一次
}

gamewindow::~gamewindow()
{
    delete ui;
}
void gamewindow::paintEvent(QPaintEvent *e){
    DrawChessboard();
    DrawItems();
    DrawItemWithMouse();

    update();
}

void gamewindow::DrawChessboard(){
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::black),2));

    //背景
    if(mIsWhichBg==1)
    {
        painter.drawPixmap(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375,QPixmap(":imgs/img/bg2.jpg"));
    }
    if(mIsWhichBg==2)
    {
        painter.drawPixmap(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375,QPixmap(":imgs/img/bg1.jpg"));
    }
    if(mIsWhichBg==3)
    {
        painter.drawPixmap(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375,QPixmap(":imgs/img/bg3.jpg"));
    }

    //画主棋盘
    for(int i=0;i<CHESS_COLUMES;i++){
        for(int j=0;j<CHESS_ROWS;j++){
            painter.drawRect((i+0.5)*RECT_WIDTH,(j+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
        }
    }
    //附加显示
    painter.drawRect(387.5,12.5,200,375);
    //黑棋
    QString BLACK="黑   棋";
    QFont font1("Courier",15);
    painter.setFont(font1);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(440,60,BLACK);

    //白棋
    QString WHITE="白   棋";
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(440,180,WHITE);

    if(mIsBlackTurn)
    {
       painter.drawText(470,110,str);
       painter.drawText(470,230,"0");
    }
    if(!mIsBlackTurn)
    {
        painter.drawText(470,230,str);
        painter.drawText(470,110,"0");
    }
}

//倒计时
bool gamewindow::countdown()
{
    temp_time--;
    str=tr("%1").arg(temp_time);

    if(temp_time==0)
    {
        timer->stop();
        QSound *startsound=new QSound(":mus/music/victory.wav");
        startsound->play();
        if(!mIsBlackTurn)
        {
            QMessageBox msgbox;
            msgbox.setText("超时！");
            msgbox.setInformativeText("黑棋胜利！");
            QPushButton* Yesbtn=msgbox.addButton(QMessageBox::Yes);
            msgbox.addButton(QMessageBox::No);
            msgbox.exec();
            mIsBlackTurn=true;
            if(msgbox.clickedButton()==Yesbtn){
                mItems.clear();
                timer->start(1000);
                temp_time=limit_time+1;
            }
            else{
                this->close();
                msgbox.close();
            }
        }
        else
        {
            QMessageBox msgbox;
            msgbox.setText("超时！");
            msgbox.setInformativeText("白棋胜利！");
            QPushButton* Yesbtn=msgbox.addButton(QMessageBox::Yes);
            msgbox.addButton(QMessageBox::No);
            msgbox.exec();
            mIsBlackTurn=true;
            if(msgbox.clickedButton()==Yesbtn){
                mItems.clear();
                timer->start(1000);
                temp_time=limit_time+1;
            }
            else{
                this->close();
                msgbox.close();
            }
        }
        return true;
    }
    return false;
}
void gamewindow::DrawItems(){
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    for(int i=0;i<mItems.size();i++){
        item Item=mItems[i];
        if(Item.mBlack){
            painter.setBrush(Qt::black);

        }
        else{
            painter.setBrush(Qt::white);
        }
        DrawChessAtPoint(painter,Item.mPt);
    }
}
void gamewindow::DrawChessAtPoint(QPainter& painter,QPoint& pt){

    QPoint ptCenter((pt.x()+0.5)*RECT_WIDTH,(pt.y()+0.5)*RECT_HEIGHT);
    painter.drawEllipse(ptCenter,RECT_WIDTH/2,RECT_HEIGHT/2);
}

void gamewindow::DrawItemWithMouse(){
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    if(mIsBlackTurn){
        painter.setBrush(Qt::black);
    }
    else{
        painter.setBrush(Qt::white);
    }

    painter.drawEllipse(mapFromGlobal(QCursor::pos()),RECT_WIDTH / 2,RECT_HEIGHT / 2);
}
int gamewindow::CountItems(item Item,QPoint ptDirection){
    int n=0;
    Item.mPt+=ptDirection;

    while(mItems.contains(Item)){
        n++;
        Item.mPt+=ptDirection;
    }
    return n;
}
void gamewindow::mousePressEvent(QMouseEvent*e){

    QPoint pt;
    pt.setX( (e->pos().x() ) / RECT_WIDTH);
    pt.setY( (e->pos().y() ) / RECT_HEIGHT);

    //判断落子处是否有棋子
    for(int i=0;i<mItems.size();i++){
            item Item=mItems[i];
            if(Item.mPt==pt){
                return;
            }
        }

    item Item(pt,mIsBlackTurn);
    mItems.append(Item);
    count++;
    QSound *startSound=new QSound(":mus/music/itemsound.wav");
    startSound->play();
    temp_time=limit_time;

    int nLeft=CountItems(Item,QPoint(-1,0));
    int nLeftUp=CountItems(Item,QPoint(-1,-1));
    int nLeftDown=CountItems(Item,QPoint(-1,1));
    int nRightUp=CountItems(Item,QPoint(1,-1));
    int nRight=CountItems(Item,QPoint(1,0));
    int nRightDown=CountItems(Item,QPoint(1,1));
    int nUp=CountItems(Item,QPoint(0,-1));
    int nDown=CountItems(Item,QPoint(0,1));

        if((nLeft+nRight)>=4||(nUp+nDown)>=4||(nLeftUp+nRightDown)>=4||(nLeftDown+nRightUp)>=4){
            timer->stop();
            QMessageBox msgbox;
            QSound *startsound=new QSound(":mus/music/victory.wav");
            startsound->play();
            msgbox.setText("游戏结束");
            if(mIsBlackTurn){
                msgbox.setInformativeText("黑棋胜利！");
            }
            else{
                msgbox.setInformativeText("白棋胜利！");
            }
            QPushButton* Yesbtn=msgbox.addButton(QMessageBox::Yes);
            msgbox.addButton(QMessageBox::No);
            msgbox.exec();
            if(msgbox.clickedButton()==Yesbtn){
                mItems.clear();
                timer->start(1000);
                temp_time=limit_time+1;
                mIsBlackTurn=true;
            }
            else{
                mIsBlackTurn=true;
                this->close();
                msgbox.close();
            }

            return;
        }

    mIsBlackTurn = !mIsBlackTurn;
}

void gamewindow::on_pushButton_clicked()
{
    mIsWhichBg++;
    if(mIsWhichBg==4)
    {
        mIsWhichBg=1;
    }
}

void gamewindow::on_pushButton_2_clicked()
{
    if(count==0)
    {
        QString mytitle="消息框";
        QString myinfo="还未开局！";
        QMessageBox::information(this,mytitle,myinfo);
    }
    else
    {
    QPoint pt1(1000,1000);
    mItems[count-1].mPt=pt1;
    mIsBlackTurn=!mIsBlackTurn;
    temp_time=limit_time+1;
    }
}

void gamewindow::on_pushButton_3_clicked()
{
    if(count==0)
    {
        QString mytitle="消息框";
        QString myinfo="还未开局！";
        QMessageBox::information(this,mytitle,myinfo);
    }
    else
    {
    mItems.clear();
    mIsBlackTurn=true;
    temp_time=limit_time+1;
    update();
    }
}


